<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  
</body>
<script>
  function debounce(fn, ms) {
    let timer
    return (...args) => {
      clearTimeout(timer)
      timer = setTimeout(_=> {
        fn(...args)
      }, ms)
    }
  }
  // 特点：函数一开始会立即触发
  function throttle(fn, ms) {
    let stamp = 0
    return (...args) => {
      let now = new Date()
      if (now - stamp >= ms) {
        fn(...args)
        stamp = now
      }
    }
  }
  // 特点：函数最后会再触发一次
  function throttle(fn, ms) {
    let timer
    return (...args) => {
      if (!timer) {
        timer = setTimeout(_=> {
          timer = null
          fn(...args)
        }, ms)
      }
    }
  }

  function fn(e) {
    console.log(e);
  }
  // window.onmousemove = debounce(fn, 500)
  window.onmousemove = throttle(fn, 1000)
</script>
</html>